import request from '@/utils/request'

const logService = {
  // 获取所有日志
  async getLogs() {
    try {
      const response = await request({
        url: '/Log',
        method: 'get'
      })
      return response
    } catch (error) {
      console.warn('日志API不可用，返回空数据:', error.message)
      // 返回空数据而不是抛出错误
      return {
        code: 200,
        message: '操作成功',
        data: {
          $values: []
        }
      }
    }
  },

  // 根据ID获取日志
  async getLogById(id) {
    try {
      const response = await request({
        url: `/Log/${id}`,
        method: 'get'
      })
      return response
    } catch (error) {
      console.warn('日志API不可用:', error.message)
      return {
        code: 404,
        message: '日志不存在',
        data: null
      }
    }
  },

  // 创建日志
  async createLog(data) {
    try {
      const response = await request({
        url: '/Log',
        method: 'post',
        data
      })
      return response
    } catch (error) {
      console.warn('创建日志失败:', error.message)
      return {
        code: 500,
        message: '创建日志失败',
        data: null
      }
    }
  },

  // 更新日志
  async updateLog(id, data) {
    try {
      const response = await request({
        url: `/Log/${id}`,
        method: 'put',
        data
      })
      return response
    } catch (error) {
      console.warn('更新日志失败:', error.message)
      return {
        code: 500,
        message: '更新日志失败',
        data: null
      }
    }
  },

  // 删除日志
  async deleteLog(id) {
    try {
      const response = await request({
        url: `/Log/${id}`,
        method: 'delete'
      })
      return response
    } catch (error) {
      console.warn('删除日志失败:', error.message)
      return {
        code: 500,
        message: '删除日志失败',
        data: null
      }
    }
  }
}

export default logService 